package com.interview01;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Stream;

/**
 * Copyright (C), 2018-2020
 * FileName: Demo1
 * Author:   kongfanyu
 * Date:     2020/12/3 17:30
 */
public class Demo1 {
    static Object objectLock = new Object();
    static Lock lock = new ReentrantLock();
    static Condition condition = lock.newCondition();
    public static void main(String[] args) {
        //飞天舞者
        List<String> list = Arrays.asList("迪丽热巴","古力娜扎","哈妮克孜","佟丽娅");
        Stream<String> stream = list.stream().filter(e -> e.length() == 3);
        stream.forEach(System.out::println);
    }

    private static void parkTest() {


        Thread a = new Thread(() ->{
            //先让B线程执行，发出通知，A线程还是可以正常结束
            try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
            System.out.println(Thread.currentThread().getName()+"开始执行....");
            LockSupport.park();
            System.out.println(Thread.currentThread().getName()+"结束执行....");
        },"A");
        a.start();
        //A线程先执行，阻塞，B线程3秒后执行，成功通知A线程。
        //try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }

        Thread b = new Thread(() ->{
            LockSupport.unpark(a);
            System.out.println(Thread.currentThread().getName()+"发出通知....");
        },"B");
        b.start();
    }

    private static void conditionTest() {
        new Thread( () ->{
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"开始执行...");
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"结束执行...");
            }finally {
                lock.unlock();
            }
        },"A").start();

        new Thread( () ->{
            lock.lock();
            try {
                condition.signal();//唤醒
                System.out.println(Thread.currentThread().getName()+"开始唤醒...");
            }finally {
                lock.unlock();
            }
        },"B").start();
    }

    private static void test1() {
        new Thread( () ->{
            synchronized (objectLock){
                System.out.println(Thread.currentThread().getName()+"开始执行");
                try {
                    objectLock.wait();//等待
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"被唤醒");
            }
        },"A").start();

        //让唤醒线程先执行，就出现问题了，A线程一直处于等待状态，无法唤醒
        try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }

        new Thread( () ->{
            synchronized (objectLock){
                objectLock.notify();//唤醒线程
                System.out.println(Thread.currentThread().getName()+"发出通知..");
            }
        },"B").start();
    }
}
